//
//  GroundLayer.m
//  HeroNuts
//
//  Created by Magic on 11-3-31.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "GroundLayer.h"
#import "HeroNuts.h"
#import "HNLogicPosition.h"
#import "GameConfig.h"
#import "GameScene.h"

@implementation GroundLayer

@synthesize selected = selected_;
@synthesize mapGrid = mapGrid_;

- (void) dealloc
{
	[mapGrid_ release]; 
	[super dealloc];
}


-(id) init
{
	if((self = [super init])) 
	{
                
        
		CCSprite* ground = [CCSprite spriteWithFile:@"battle_ground_1.png"];
		
		ground.anchorPoint = ccp(0.0f, 1.0f);
		
		ground.position = ccp(0, 600);
		[self addChild:ground];
		
		
		
		mapGrid_ = [[HNMapGrid alloc] initWithParent:self];
  
	}
	return self;
}


-(void) prepareForNextActor:(HNActor*)actor
{
    self.isTouchEnabled = YES;
	NSMutableArray* array = [[HNGroundLogic sharedGroundLogic] getWalkAreaFrom:actor.position];
	[mapGrid_ highlightGridwithArray:array];
}

#pragma mark Touches
- (CGPoint) changeToLogicPosition: (CGPoint)position
{
    int pixelX = (int)position.x - 80;
    int pixelY = (int)position.y - 50;
    
    int row = pixelY / kMapGridDistanceY;
    
    if (row & 1) {
        pixelX += 22;
    }
    
    int col = (int)pixelX / kMapGridDistanceX;

    return CGPointMake(col , row);
}

/*
- (CGPoint) changeToRealPosition: (CGPoint)position
{
    //80 93
    CGPoint distance = [HNLogicPosition distanceInPixelsFrom:ccp(0,0) to:position];
    CGPoint result = ccpAdd(ccp(80,93), distance);
    
    return result;
    
}*/

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event	 
{ 
    CGPoint location = [touch locationInView: [touch view]];		
    location = [[CCDirector sharedDirector] convertToGL: location];    
    CGPoint logicPosition = [self changeToLogicPosition:location];
    
	if (logicPosition.x<0 || logicPosition.x>14 || logicPosition.y<0 || logicPosition.y>10) {
        return NO;
    }
     
    [[GameScene sharedGameScene].effectLayer.touchProcessor hnTouchBeganAtPosition:logicPosition];
    [mapGrid_ hnTouchBeganAtPosition:logicPosition];
	
	return  YES;
}



- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint location = [touch locationInView: [touch view]];		
    location = [[CCDirector sharedDirector] convertToGL: location];    
    CGPoint logicPosition = [self changeToLogicPosition:location];
    
    if (logicPosition.x<0 || logicPosition.x>14 || logicPosition.y<0 || logicPosition.y>10) {
        return;
    }
     
    [[GameScene sharedGameScene].effectLayer.touchProcessor hnTouchMovedToPosition:logicPosition];
    [mapGrid_ hnTouchMovedToPosition:logicPosition];
     
}


- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{    
    CGPoint location = [touch locationInView: [touch view]];		
    location = [[CCDirector sharedDirector] convertToGL: location];    
    CGPoint logicPosition = [self changeToLogicPosition:location];
    
    [[GameScene sharedGameScene].effectLayer.touchProcessor hnTouchEndedAtPosition:logicPosition];
    [mapGrid_ hnTouchEndedAtPosition:logicPosition];
}


-(void) registerWithTouchDispatcher {
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:-1 swallowsTouches:YES];
}

@end
